/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bground.game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.util.HashMap;
import javax.imageio.ImageIO;

/**
 *
 * @author s6_kustan_e
 */
public class GL {

    static final int TILESIZE = 50;
    static final int FIELDSIZE = 11;
    static HashMap<String, BufferedImage> cache;
    static public enum Direction {
        no, up, right, down, left
    }
    static public BufferedImage getGraphics(String name) {
        if (cache == null) {
            cache = new HashMap<String, BufferedImage>();
        }
        try {
            if (cache.containsKey(name + "_u")) {
                return cache.get(name + "_u");
            }
            cache.put(name + "_u", ImageIO.read(GL.class.getResourceAsStream("res/" + name + ".png")));
            return cache.get(name + "_u");
        } catch (Exception e) {
            BufferedImage i = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = i.createGraphics();
            g.fillRect(0, 0, 50, 50);
            g.setColor(Color.red);
            g.drawLine(0, 0, 50, 50);
            g.drawLine(0, 50, 50, 0);
            g.dispose();
            return i;
        }
    }

    /**
     * Rotation, initial direction is up
     * @param name
     * @param direction
     * @return
     */
    static public BufferedImage getGraphics(String name, Direction direction) {
        BufferedImage i = getGraphics(name);
//        Graphics2D g = i.createGraphics();
        double angle = 0;
        name += "_" + direction.name().substring(0, 1);
        if (cache.containsKey(name)) {
            return cache.get(name);
        }
        switch (direction) {
            case no:
                angle = 0;
                break;
            case up:
                angle = 0;
                break;
            case left:
                angle = 3 * Math.PI / 2;
                break;
            case down:
                angle = Math.PI;
                break;
            case right:
                angle =  Math.PI / 2;
                break;

        }
        AffineTransform at = new AffineTransform();
        at.rotate(angle,TILESIZE/2,TILESIZE/2);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
        i = bio.filter(i, null);
        cache.put(name, i);
        return i;
    }

    private GL() {
    }

}
